﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TienIch.Model;
using TienIch.Utils;
using TienIch.Business;
using TienIch;
using DevExpress.XtraEditors;
namespace TienIch
{
    public partial class frmChangePassword : FormBase
    {
        public frmChangePassword()
        {
            InitializeComponent();
        }

        private void frmChangePassword_Load(object sender, EventArgs e)
        {
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!yes)
            {
                return;
            }
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txtNewPass.Text))
            {
                errorProvider1.SetError(txtNewPass, "Bạn không thể bỏ trống trường này");
                return;
            }
            UsersModel model = (UsersModel)UsersBO.Instance.FindByPKBase(Global.UserID);
            model.PasswordHash = MD5.EncryptPassword(txtNewPass.Text.Trim());
            UsersBO.Instance.Update(model);
            XtraMessageBox.Show("Đổi mật khẩu thành công!","Thông báo");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        bool yes = true;
        private void txtOldPass_TextChanged(object sender, EventArgs e)
        {

            if (MD5.EncryptPassword(txtOldPass.Text.Trim()) != ((UsersModel)UsersBO.Instance.FindByPKBase(Global.UserID)).PasswordHash)
            {
                errorProvider1.SetError(txtOldPass, "Mật khẩu cũ không đúng");
                yes = false;
            }
            else
            {
                errorProvider1.Clear();
                yes = true;
            }
        }

        
    }
}
